package com.wsd.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsd.bean.ProductRank;
import com.wsd.bean.Products;
import com.wsd.dao.ProductRankDaoInterface;

/**
 * A data access object (DAO) providing persistence and search support for
 * ProductRank entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.wsd.bean.ProductRank
 * @author MyEclipse Persistence Tools
 */

public class ProductRankDAO extends HibernateDaoSupport implements ProductRankDaoInterface {
	private static final Log log = LogFactory.getLog(ProductRankDAO.class);

	protected void initDao() {
		// do nothing
	}

	public void save(ProductRank transientInstance) {
		log.debug("saving ProductRank instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ProductRank persistentInstance) {
		log.debug("deleting ProductRank instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ProductRank findById(java.lang.Integer id) {
		log.debug("getting ProductRank instance with id: " + id);
		try {
			ProductRank instance = (ProductRank) getHibernateTemplate().get(
					"com.wsd.bean.ProductRank", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public List findByExample(Object exampleEntity) {
		log.debug("finding by Products instances");
		try {
			return getHibernateTemplate().findByExample(exampleEntity);
		} catch (RuntimeException re) {
			log.error("find example failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<ProductRank> findAll() {
		log.debug("finding all ProductRank instances");
		try {
			String queryString = "from ProductRank";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ProductRankDaoInterface#checkExist(int, java.lang.Short)
	 */
	public boolean checkExist(int id, Short model) {
		String sql = "from ProductRank where products.PId = :id and BModel = :model";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setInteger("id", id);
		query.setShort("model", model);
		return query.list().size() > 0;
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ProductRankDaoInterface#rankProducts(java.lang.Short, int, int)
	 */
	public List<Products> rankProducts(Short model, int start, int limit) {
		String sql = "select p.products from ProductRank p where p.BModel = :model order by p.BId desc";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setShort("model", model);
		query.setFirstResult(start);
		query.setMaxResults(limit);
		return query.list();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ProductRankDaoInterface#rankProductsCount(java.lang.Short)
	 */
	public int rankProductsCount(Short model) {
		String sql = "select count(*) from ProductRank where BModel = :model";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setShort("model", model);
		Long count = (Long) query.uniqueResult();
		return count.intValue();
	}

	/* (non-Javadoc)
	 * @see com.wsd.dao.ProductRankDaoInterface#removeRank(int, java.lang.Short)
	 */
	public void removeRank(int id, Short model) {
		String sql = "delete ProductRank where BModel = :model and products.PId = :id";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql);
		query.setShort("model", model);
		query.setInteger("id", id);
		query.executeUpdate();
	}
}